package com.czk.api.getData;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/6/2 16:12
 */
public class GetData {
    private static Connection connection = null;
    private static Admin admin = null;

    static {
        // 静态代码块初始化
        // 创建配置类
        Configuration configuration = HBaseConfiguration.create();
        // zookeeper连接
        configuration.set("hbase.zookeeper.quorum", "192.168.25.133");
        // 端口
        configuration.set("hbase.zookeeper.property.clientPort", "2181");
        //
        try {
            connection = ConnectionFactory.createConnection(configuration);
            admin = connection.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭连接
     */
    public static void close() {
        try {
            if (admin != null) {
                admin.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }

    }

    public static void main(String[] args) throws IOException {
        // getData("stu", "1001", null, null);
        // getData("stu", "1001", "info1", null);
        getData("stu", "1001", "info1", "name");
    }

    /**
     * 获取单行数据
     *
     * @param tableName 表名
     * @param rowKey    行
     * @param cf        列族
     * @param cn        列
     * @throws IOException
     */
    public static void getData(String tableName, String rowKey, String cf, String cn) throws IOException {
        // 校验参数
        if (tableName == null || rowKey == null) {
            throw new IllegalArgumentException("参数错误");
        }
        // 拿到表
        Table table = connection.getTable(TableName.valueOf(tableName));
        // 创建get对象
        Get get = new Get(Bytes.toBytes(rowKey));
        // 指定列族
        if (cf != null) {
            // 指定列名
            // 列名不能脱离列族单独存在
            if (cn != null) {
                get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));
            } else {
                // 指定列族
                get.addFamily(Bytes.toBytes(cf));
            }

        }

        // 获取数据
        Result result = table.get(get);
        // 解析result,result包含很多cell
        Cell[] cells = result.rawCells();
        // 遍历对象
        for (Cell cell : cells) {
            // 打印数据
            byte[] bytes = CellUtil.cloneFamily(cell);
            // 转为string
            // 列族
            String CF = Bytes.toString(bytes);
            // 列名
            String CN = Bytes.toString(CellUtil.cloneQualifier(cell));
            // 值
            String value = Bytes.toString(CellUtil.cloneValue(cell));
            System.out.println("CF=" + CF);
            System.out.println("CN=" + CN);
            System.out.println("value=" + value);
            System.out.println(cell.getTimestamp());
        }
        close();
        table.close();
    }
}
